
;-------------------
; Created by Kaitnieks
; Rat fighter 1
; Start in melzars maze
;
; Notes: About %OkToMove
;  When %OkToMove is 1 it will
;   attack any rat that appears
;   or moves and set it to 0
;  When %OkToMove is 0 it won't
;   attack. This happens when you
;   are fighting already or burying
;   bones. After you finish doing it
;   you will attack again.

;Don't trace events
SetTraceMode(1)
GoToIfQuestion(@Filter,"Do you have chat messages blocked?")
GoTo(@Beginning)
@Filter:
OnChatMessage(@RunAndHideChat)

;-- Set up all events
@Beginning:
OnCoordsChanged(@CoordsChange,%oldX,%oldY)

OnServerMessage(@ServMes)

OnNPCAppeared(@NewNPC,%ID,%NPCX,%NPCY,%NPCType)
OnNPCMoved(@MovNPC,%ID,%NPCX,%NPCY,%NPCType)
OnItemAppeared(@NewItem,%ItemID,%X,%Y)
SetVarNum(%OkToMove,1)
OnStatsChanged(@StatsChange,%Stat)
SetVarNum(%LastMonst,0)
SetVarNum(%LM,0)
SetVarNum(%IdleLoops,0)



;--- Main Loop
@LoopStart:
Wait(70)

;If we're in the small room behind door, come out
SetVarMyX(%x)
SetVarMyY(%y)
GoToIfVarAboveNum(@CheckY,%y,636)

;If we can't move (because we're chasing NPC)
;then don't dig bones
GoToIfVarEqualNum(@NoDigging,%OkToMove,0)
;If we have bones, dig them
GoToIfInInventory(@DigBones,20)
@NoDigging:
AddVarNum(%IdleLoops,1)
;If we didn't attack for 3 loops,
;start to move again and attack other NPCs
GoToIfVarAboveNum(@AttackLM,%IdleLoops,3)
GoTo(@LoopStart)
;--- End of Main Loop



;---Pick up bones
@GrabBones:
ActionVarVar(%X,%Y)
TakeItemVarVar(%X,%Y,20)
Wait(20)
GoTo(@DontGrabBones)

;---Pick up coins
@GrabCoins:
ActionVarVar(%X,%Y)
TakeItemVarVar(%X,%Y,10)
Wait(20)
GoTo(@DontGrabCoins)

;--Pick up chaos runes
;quite useless, rats don't drop them
@GrabChaos:
ActionVarVar(%X,%Y)
TakeItemVarVar(%X,%Y,41)
Wait(20)
GoTo(@DontGrabChaos)


;--- Dig all bones
@DigBones:
GoToIfVarEqualNum(@LoopStart,%OkToMove,0)
@DigBonesLoop:
SetVarNum(%OkToMove,0)
UseItemByID(20)
Wait(20)
GoToIfInInventory(@DigBonesLoop,20)
SetVarNum(%OkToMove,1)
Debug("Bones ar buried, can move again")
GoTo(@LoopStart)
-

;-- Reset all stats, make it to attack next moving NPC
@AttackLM:
Debug("Was idling, let's just attack")
SetVar(%LastMonst,%LM)
SetVarNum(%IdleLoops,0)
SetVarNum(%OkToMove,1)
GoTo(@LoopStart)


;-- NPC appears
@MovNPC:
GoTo(@NewNPC)

@NewNPC:
SetVarNum(%LastMonst,0)
SetVarMyHP(%HP)
--- there are 2 kinds of rats
;-- Change 177 and 47 to the type of
;-- NPCs that interest you
;-- To find out NPC type use EvenTest script
GoToIfVarEqualNum(@Fight,%NPCType,177)
GoToIfVarEqualNum(@Fight,%NPCType,47)
ERet()

@Fight:
SetVar(%LM,%ID)
-Debug("My HP is:")
-DebugVar(%HP)
; Attack if health is more than 10
GoToIfVarAboveNum(@Fight2,%HP,10)
; or if it's unknown yet
GoToIfVarEqualNum(@Fight2,%HP,0)
SetVar(%LastMonst,%ID)
Debug("We can't attack atm, let's wait for heal")
ERet()
@Fight2:
;Don't attack while burying bones 
;or fighting other NPC
GoToIfVarEqualNum(@Fight3,%OkToMove,1)
-Debug("Can't move atm")
ERet()
@Fight3:

;Attack NPC only that is 15 or less
;tiles away from you (check X coord)
SetVarMyX(%MaxX)
AddVarNum(%MaxX,15)
SetVarMyX(%MinX)
AddVarNum(%MinX,-15)
GoToIfVarBetween(@Fight4,%NPCX,%MinX,%MaxX)
Debug("X is too far")
DebugVar(%MinX,%MaxX,%NPCX)
ERet()
@Fight4:

;Attack NPC only that is 15 or less
;tiles away from you (check Y coord)
SetVarMyY(%MaxY)
AddVarNum(%MaxY,15)
SetVarMyY(%MinY)
AddVarNum(%MinY,-15)
GoToIfVarBetween(@Fight5,%NPCY,%MinY,%MaxY)
Debug("Y is too far")
DebugVar(%MinY,%MaxY,%NPCY)
ERet()
@Fight5:

;Check if NPC is in maze by checking
;absolute coords. Don't attack rats
;outside maze (check x coord)
GoToIfVarBetweenNum(@Fight6,%NPCX,289,301)
ERet()
@Fight6:

;Check if NPC is in maze by checking
;(check y coord)
GoToIfVarBetweenNum(@Fight7,%NPCY,627,637)
ERet()
@Fight7:

;Chase that NPC, don't bury bones
;or anything now
SetVarNum(%OkToMove,0)
Debug("cant move")
DebugVar(%ID,%NPCX,%NPCY,%NPCType)
SetCoordsNearer(%NPCX,%NPCY)
SetCoordsNearer(%NPCX,%NPCY)
ActionVarVar(%NPCX,%NPCY)
;Attack poor rat
AttackNPCVar(%ID)
SetVarNum(%IdleLoops,0)
ERet()


;--- New item appeared
@NewItem:
;-- If we're not fighting or burying bones
; then take the item
GoToIfVarEqualNum(@TakeItems,%OkToMove,1)
ERet()
@TakeItems:
-Debug("Item appears")
-DebugVar(%ItemID,%X,%Y)
SetVarMyX(%MX)
SetVarMyY(%MY)
;Only take the item if it's right under my feet
GoToIfVarNotEqual(@DontGrabAnything,%X,%MX)
GoToIfVarNotEqual(@DontGrabAnything,%Y,%MY)
;-- Only take certain items
GoToIfVarNotEqualNum(@DontGrabBones,%ItemID,20)
GoTo(@GrabBones)
@DontGrabBones:
GoToIfVarNotEqualNum(@DontGrabCoins,%ItemID,10)
GoTo(@GrabCoins)
@DontGrabCoins:
GoToIfVarNotEqualNum(@DontGrabChaos,%ItemID,41)
GoTo(@GrabChaos)
@DontGrabChaos:
@DontGrabAnything:
ERet()


;--- OnStatChaged
@StatsChange:
@CanMove:
;If my HitPoints skill changed, it means
;fight with rat is over
GoToIfVarEqualNum(@SetCanMove,%Stat,3)
ERet()
@SetCanMove:
SetVarNum(%OkToMove,1)
Debug("can move")
ERet()


;--- Teleport checker
@CoordsChange:
GoTo(@CheckTeleport)
@CheckY:
GoTo(@CheckGoUp)
GoTo(@CheckTeleport)
@CheckGoUp:
Debug("Going back down")
MoveTo(298,637,0,-1)
GoTo(@LoopStart)

@CheckTeleport:
SetVarMyX(%x)
SetVarMyY(%y)
SetVar(%diff,%oldX)
AddVarNum(%diff,40)
GoToIfVarAbove(@RunAndHideTele,%x,%diff)
SetVar(%diff,%oldX)
AddVarNum(%diff,-40)
GoToIfVarBelow(@RunAndHideTele,%x,%diff)

SetVar(%diff,%oldY)
AddVarNum(%diff,40)
GoToIfVarAbove(@RunAndHideTele,%y,%diff)
SetVar(%diff,%oldY)
AddVarNum(%diff,-40)
GoToIfVarBelow(@RunAndHideTele,%y,%diff)
ERet()


;--- OnServerMessage
@ServMes:
DebugLastServerMessage()
;-- If someone stole your rat
GoToIfLastServerMessageIs(@ResetGo,"I can't get close enough")
;-- or your rat runs away
GoToIfLastServerMessageIs(@ResetGo,"Your opponent is retreating")
ERet()
;-- then make your char to choose another rat
@ResetGo:
Debug("Forget this monster")
SetVarNum(%OkToMove,1)
SetVarNum(%LastMonst,0)
ERet()


;--- Sh*t happened lol
@RunAndHideTele:
Wait(25,30)
Say(16 13 05 )
Debug("Saying 'Wtf'")
Wait(10)
Debug("Ooops we just got teleported! Must be an admin!!!")
DebugVar(%x,%y,%oldX,%oldY)
GoTo(@RunAndHide)


@RunAndHidePick:
Debug("Ooops we just lost pickaxe! Must be an admin!!!")
GoTo(@RunAndHide)

@RunAndHideChat:
Debug("Ooops someone is talking in the game. If chat messages are blocked - its an admin!!!")
GoTo(@RunAndHide)

@RunAndHide:
;-- This is worth saving
SaveScreenshot()
StopAndLogOut()
Wait(1)



